﻿using System.Reflection;

namespace Borg
{
    internal class PlugInSlotsTask : Task<ExtensionInfo>
    {
        private readonly ExtensionTypeInfo _pluggingEti;
        private readonly ExtensionInfo _pluggingEi;

        private readonly bool _isManual;

        public override string Name
        {
            get
            {
                if (_pluggingEti != null)
                    return string.Format("PlugInSlots({0}, {1})", obj.Name, _pluggingEti.Name);

                return _pluggingEi == null
                    ? string.Format("PlugInSlots({0})", obj.Name)
                    : string.Format("PlugInSlots({0}, {1})", obj.Name, _pluggingEi.Name);
            }
        }

        public PlugInSlotsTask(ExtensionInfo obj, ExtensionInfo pluggingEi,
            bool isManual, uint taskGroupId, Assembly initiator)
            : base(obj, taskGroupId, initiator)
        {
            _pluggingEi = pluggingEi;
            _isManual = isManual;
        }

        public PlugInSlotsTask(ExtensionTypeInfo pluggingEti, ExtensionInfo obj,
            bool isManual, uint taskGroupId, Assembly initiator)
            : base(obj, taskGroupId, initiator)
        {
            _pluggingEti = pluggingEti;
            _isManual = isManual;
        }

        public override void Execute()
        {
            if (_pluggingEti != null)
                obj.InternalPlugInSlots(_pluggingEti, _isManual, taskGroupId, initiator);
            else if (_pluggingEi == null)
                obj.InternalPlugInSlots(taskGroupId, initiator);
            else
                obj.InternalPlugInSlots(_pluggingEi, _isManual, taskGroupId, initiator);
        }

        public override string ToString()
        {
            if (_pluggingEti != null)
                return string.Format(
                    "{0}: {1}.InternalPlugInSlots({2}, {3})",
                    (object)taskGroupId, (object)obj, (object)_pluggingEti, (object)(_isManual ? 1 : 0));

            var str = _pluggingEi != null
                             ? string.Format(
                                 "{0}: {1}.InternalPlugInSlots({2}, {3})",
                                 (object)taskGroupId, (object)obj, (object)_pluggingEi, (object)(_isManual ? 1 : 0))
                             : string.Format(
                                 "{0}: {1}.InternalPlugInSlots({2})",
                                 taskGroupId, obj, _isManual ? 1 : 0);

            return str;
        }
    }
}

